package com.wxj.book.cms.controller;

import com.github.pagehelper.PageInfo;
import com.wxj.book.cms.common.ServerResponse;
import com.wxj.book.cms.pojo.Major;
import com.wxj.book.cms.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MajorController {
    @Autowired
    private MajorService majorService;

    /**
     * 查询所有的专业信息
     */
    @RequestMapping("major/getAll")
    public String getAllMajor(@RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                              ModelMap modelMap) {
        PageInfo<Major> pageInfo = majorService.getAll(pageNum, pageSize);
        modelMap.addAttribute("pageInfo", pageInfo);
        return "major";
    }

    /**
     * 更新或者添加
     */
    @PostMapping("major/updateMajor")
    @ResponseBody
    public ServerResponse updateMajor(Major major) {
        if (major == null) {
            return ServerResponse.createByErrorMessage("操作失败");
        }
        int result = majorService.updateMajor(major);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("操作成功");
        }
        return ServerResponse.createByErrorMessage("操作失败");
    }

    /**
     *  根据id删除
     */
    @RequestMapping("major/deleteById")
    @ResponseBody
    public ServerResponse deleteById(@RequestParam("majorId") Integer majorId) {
        if (majorId == null) {
            return ServerResponse.createByErrorMessage("删除失败");
        }
        int result = majorService.deleteById(majorId);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("删除成功");
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }
}
